package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询班级
     *
     * @param name     班级名称
     * @param begin    开始时间
     * @param end      结束时间
     * @param page     页码
     * @param pageSize 页面总记录数
     * @return 分页查询结果
     */
    @Override
    public PageResult<Clazz> getByPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        //创建分页查询结果对象
        PageResult<Clazz> pageResult = new PageResult<>();

        //利用page刚发实现分页
        Page<Clazz> pages = new Page<>(page,pageSize);
        pages.addOrder(OrderItem.desc("update_time"));

        //查询条件
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !name.isEmpty(), Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getCreateTime, begin, end);

        pages = page(pages);

        //根据班级中的master_id查询出班主任名称
        List<Clazz> clazzs = pages.getRecords();
        List<Long> empIds = clazzs.stream().map(Clazz::getMasterId).toList();

        List<Emp> emps = empService.listByIds(empIds);
        emps.forEach(emp ->{
            clazzs.forEach(clazz -> {
               if (emp.getId().equals(clazz.getMasterId())) {
                   clazz.setMasterName(emp.getName());
               }
            });
        });
        clazzs.forEach(clazz -> {
            if (clazz.getEndDate().isBefore(LocalDate.now()) ){
                clazz.setStatus("已结课");
            } else if (clazz.getBeginDate().isAfter(LocalDate.now()) ){
                clazz.setStatus("未开课");
            }else{
                clazz.setStatus("已开班");
            }
        });

        pageResult.setTotal(pages.getTotal());
        pageResult.setRows(clazzs);
        //将查询结果返回
        return pageResult;
    }

    /**
     * 添加班级
     *
     * @param clazz 班级对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClazz(Clazz clazz) {
        //添加班级
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }



    /**
     * 修改班级
     *
     * @param clazz 班级对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }
}
